package com.qf.controller;

import com.qf.service.DtsGoodsOrderService;
import com.qf.service.DtsOrderService;
import com.qf.service.DtsUserService;
import com.qf.util.ResponseUtil;
import com.qf.vo.DayStatis;
import com.qf.vo.GoodsCountVo;
import com.qf.vo.OrderCountVo;
import com.qf.vo.StatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@CrossOrigin("*")
@RestController
@RequestMapping("/admin/stat")
public class AdminStatController {

    @Autowired
    DtsUserService userService;

    @Autowired
    DtsOrderService orderService;

    @Autowired
    DtsGoodsOrderService goodsOrderService;

    private static final int STATIS_DAYS_RANG = 30;

    @RequestMapping("/user")
    public Object user(){

        List<DayStatis> userStatis = userService.queryDayStatis(STATIS_DAYS_RANG);
        List<Map> list = new ArrayList<>();
        for (DayStatis userStati : userStatis) {
            Map<String, Object> map = new HashMap<>();
            map.put("day",userStati.getDayStr());
            map.put("users",userStati.getCnts());
            list.add(map);
        }
        StatVo statVo = new StatVo();

        String [] s = {"day","users"};

        statVo.setColumns(s);
        statVo.setRows(list);

        return ResponseUtil.ok(statVo);
    }

    @RequestMapping("/order")
    public Object order(){
        List<OrderCountVo> orderCountVos = orderService.queryOrderCountVo(STATIS_DAYS_RANG);
        List<Map<String, Object>> rows = orderCountVos.stream().map(oc -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put("day", oc.getDay());
            map.put("orders", oc.getOrders());
            map.put("customers", oc.getCustomer());
            map.put("amount", oc.getAmount());
            map.put("pcr", oc.getPcr());
            return map;
        }).collect(Collectors.toList());

        String[] columns = {"day","orders","customers","amount","pcr"};
        Map<String ,Object> data = new HashMap<>();
        data.put("columns",columns);
        data.put("rows",rows);
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/goods")
    public Object goods(){

        List<GoodsCountVo> goodsCountVos = goodsOrderService.queryGoodsCountVo(STATIS_DAYS_RANG);

        List<Map<String, Object>> rows = goodsCountVos.stream().map(gc -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put("day", gc.getDay());
            map.put("orders", gc.getOrders());
            map.put("products", gc.getProducts());
            map.put("amount", gc.getAmount());
            return map;
        }).collect(Collectors.toList());

        String[] columns = {"day","orders","products","amount"};
        Map<String ,Object> data = new HashMap<>();
        data.put("columns",columns);
        data.put("rows",rows);
        return ResponseUtil.ok(data);
    }

}
